{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 通过list构建Series",
   "id": "e8c0c24a211ebc23"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-06T02:11:56.962608Z",
     "start_time": "2025-03-06T02:11:56.256400Z"
    }
   },
   "source": "import pandas as pd",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T02:11:58.230161Z",
     "start_time": "2025-03-06T02:11:58.213706Z"
    }
   },
   "cell_type": "code",
   "source": [
    "ser_obj = pd.Series(range(10,20))\n",
    "print(ser_obj.head(3)) #打印头三行\n",
    "print('*' * 50)\n",
    "print(ser_obj)\n",
    "print('*' * 50)\n",
    "print(type(ser_obj))\n",
    "print('*' * 50)\n",
    "\n",
    "#获取数据和索引\n",
    "\n",
    "#获取数据\n",
    "print(ser_obj.values)\n",
    "print('*' * 50)\n",
    "print(ser_obj.index)\n",
    "print('*' * 50)\n",
    "\n",
    "#通过索引获取数据\n",
    "print(ser_obj[0])\n",
    "print('*' * 50)\n",
    "print(ser_obj[8])\n",
    "print('*' * 50)\n",
    "\n",
    "#索引与数据的对应关系不被运算结果影响\n",
    "print(ser_obj * 2)\n",
    "print('*' * 50)\n",
    "print(ser_obj > 15)\n",
    "print('*' * 50)\n",
    "\n",
    "#通过dict构建Series\n",
    "year_data = {2001:17.8,2002:20.1,2003:16.5}\n",
    "ser_obj2 = pd.Series(year_data)\n",
    "print(ser_obj2.head())\n",
    "print('*' * 50)\n",
    "print(ser_obj2.index)\n",
    "print('*' * 50)\n",
    "print(ser_obj2[2001])\n",
    "print('*' * 50)"
   ],
   "id": "cd2b364d5c3cec5e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "dtype: int64\n",
      "**************************************************\n",
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n",
      "**************************************************\n",
      "<class 'pandas.core.series.Series'>\n",
      "**************************************************\n",
      "[10 11 12 13 14 15 16 17 18 19]\n",
      "**************************************************\n",
      "RangeIndex(start=0, stop=10, step=1)\n",
      "**************************************************\n",
      "10\n",
      "**************************************************\n",
      "18\n",
      "**************************************************\n",
      "0    20\n",
      "1    22\n",
      "2    24\n",
      "3    26\n",
      "4    28\n",
      "5    30\n",
      "6    32\n",
      "7    34\n",
      "8    36\n",
      "9    38\n",
      "dtype: int64\n",
      "**************************************************\n",
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "6     True\n",
      "7     True\n",
      "8     True\n",
      "9     True\n",
      "dtype: bool\n",
      "**************************************************\n",
      "2001    17.8\n",
      "2002    20.1\n",
      "2003    16.5\n",
      "dtype: float64\n",
      "**************************************************\n",
      "Index([2001, 2002, 2003], dtype='int64')\n",
      "**************************************************\n",
      "17.8\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# name属性",
   "id": "cd363c0f804b7f4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T02:12:00.632516Z",
     "start_time": "2025-03-06T02:12:00.626955Z"
    }
   },
   "cell_type": "code",
   "source": [
    "ser_obj2.name = 'temp'\n",
    "ser_obj2.index.name = 'year'\n",
    "print(ser_obj2.head())"
   ],
   "id": "69c04446207c4104",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "year\n",
      "2001    17.8\n",
      "2002    20.1\n",
      "2003    16.5\n",
      "Name: temp, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# DataFame",
   "id": "52e46b1348422a3d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T02:12:02.184136Z",
     "start_time": "2025-03-06T02:12:02.163853Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#通过ndarray构建DataFrame\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "t = pd.DataFrame(np.arange(12).reshape((3,4)))\n",
    "print(t)\n",
    "print('*' * 50)\n",
    "\n",
    "array = np.random.randn(5,4)\n",
    "print(array)\n",
    "print('*' * 50)\n",
    "\n",
    "df_obj = pd.DataFrame(array)\n",
    "print(df_obj)\n",
    "print('*' * 50)\n",
    "\n",
    "\n",
    "#通过dict构建DataFrame\n",
    "dict_data = {'A': 1,\n",
    "            'B': pd.Timestamp('20190926'),\n",
    "            'C': pd.Series(1,index=list(range(4)),dtype='float32'),\n",
    "            'D': np.array([3] * 4,dtype='int32'),\n",
    "            'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "            'F': 'wangdao' }\n",
    "\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2)\n",
    "print('*' * 50)\n",
    "\n",
    "d1 = {\"name\":[\"xiaoming\",\"xiaogang\"],\"age\":[20,32],\"tel\":[10086,10010]} #字典套数组\n",
    "print(pd.DataFrame(d1))\n",
    "print('*' * 50)\n",
    "\n",
    "d2 =[{\"name\" : \"xiaohong\" ,\"age\" :32,\"tel\" :10010},{ \"name\":\"xiaogang\" ,\"tel\": 10000} ,{\"name\":\"xiaowang\" ,\"age\":22}] #数组套字典\n",
    "print(pd.DataFrame(d2))\n",
    "print('*' * 50)"
   ],
   "id": "f6e3ce36e3e6f13b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1   2   3\n",
      "0  0  1   2   3\n",
      "1  4  5   6   7\n",
      "2  8  9  10  11\n",
      "**************************************************\n",
      "[[-0.45672597  0.04982088 -0.33821839  0.04206072]\n",
      " [-0.14339619 -2.32589848 -0.51455712 -0.70229787]\n",
      " [-0.0444337  -1.07498143  0.28365646 -0.25792304]\n",
      " [ 1.23336973 -1.76596602  2.90809461  0.21657472]\n",
      " [ 1.39050383  0.25728385 -0.72749043  0.2173091 ]]\n",
      "**************************************************\n",
      "          0         1         2         3\n",
      "0 -0.456726  0.049821 -0.338218  0.042061\n",
      "1 -0.143396 -2.325898 -0.514557 -0.702298\n",
      "2 -0.044434 -1.074981  0.283656 -0.257923\n",
      "3  1.233370 -1.765966  2.908095  0.216575\n",
      "4  1.390504  0.257284 -0.727490  0.217309\n",
      "**************************************************\n",
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  3  Python  wangdao\n",
      "1  1 2019-09-26  1.0  3    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  3       C  wangdao\n",
      "**************************************************\n",
      "       name  age    tel\n",
      "0  xiaoming   20  10086\n",
      "1  xiaogang   32  10010\n",
      "**************************************************\n",
      "       name   age      tel\n",
      "0  xiaohong  32.0  10010.0\n",
      "1  xiaogang   NaN  10000.0\n",
      "2  xiaowang  22.0      NaN\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 通过列索引获取列数据",
   "id": "992e2a015e6c27a8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T02:12:04.376418Z",
     "start_time": "2025-03-06T02:12:04.370224Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_obj2['A'])\n",
    "print('*' * 50)\n",
    "print(type(df_obj2['A']))\n",
    "print('*' * 50)\n",
    "print(df_obj2.A)\n",
    "print('*' * 50)"
   ],
   "id": "e181d72403d46142",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "1    1\n",
      "2    1\n",
      "3    1\n",
      "Name: A, dtype: int64\n",
      "**************************************************\n",
      "<class 'pandas.core.series.Series'>\n",
      "**************************************************\n",
      "0    1\n",
      "1    1\n",
      "2    1\n",
      "3    1\n",
      "Name: A, dtype: int64\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 增加列数据",
   "id": "11be113f0a8dd5ef"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T02:12:08.546325Z",
     "start_time": "2025-03-06T02:12:08.533381Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_obj2)\n",
    "print('*' * 50)\n",
    "df_obj2['G'] = df_obj2['D'] + 4\n",
    "print(df_obj2)\n",
    "print('*' * 50)"
   ],
   "id": "b021240f77747ba6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  3  Python  wangdao\n",
      "1  1 2019-09-26  1.0  3    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  3       C  wangdao\n",
      "**************************************************\n",
      "   A          B    C  D       E        F  G\n",
      "0  1 2019-09-26  1.0  3  Python  wangdao  7\n",
      "1  1 2019-09-26  1.0  3    Java  wangdao  7\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao  7\n",
      "3  1 2019-09-26  1.0  3       C  wangdao  7\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 删除列",
   "id": "8a3de8210152042b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T02:12:10.035080Z",
     "start_time": "2025-03-06T02:12:10.026106Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_obj2)\n",
    "print('*' * 50)\n",
    "del(df_obj2['G'])\n",
    "print(df_obj2)\n",
    "print('*' * 50)"
   ],
   "id": "72ec429481310f97",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F  G\n",
      "0  1 2019-09-26  1.0  3  Python  wangdao  7\n",
      "1  1 2019-09-26  1.0  3    Java  wangdao  7\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao  7\n",
      "3  1 2019-09-26  1.0  3       C  wangdao  7\n",
      "**************************************************\n",
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  3  Python  wangdao\n",
      "1  1 2019-09-26  1.0  3    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  3       C  wangdao\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# index指引行索引名",
   "id": "80420cb14728154b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T02:22:13.190478Z",
     "start_time": "2025-03-06T02:22:13.182263Z"
    }
   },
   "cell_type": "code",
   "source": [
    "ser_obj = pd.Series(range(5),index = ['a','b','c','d','e'])\n",
    "print(ser_obj.head())\n",
    "print('*' * 50)\n",
    "\n",
    "#另一种写法\n",
    "ser_obj = pd.Series(range(5),index = list('abcde'))\n",
    "print(ser_obj.head())\n",
    "print('*' * 50)\n",
    "\n",
    "#行索引\n",
    "print(ser_obj['b'])\n",
    "print('*' * 50)\n",
    "print(ser_obj[2])\n",
    "print('*' * 50)\n",
    "\n",
    "#切片索引\n",
    "print(ser_obj[1:3])\n",
    "print('*' * 50)\n",
    "# print(ser_obj['a':'d']) '''两边均闭合'''\n",
    "print('*' * 50)\n",
    "\n",
    "#不连续索引\n",
    "print(ser_obj[[1,3,4]])\n",
    "print('*' * 50)\n",
    "print(ser_obj[['a','d']])\n",
    "print('*' * 50)"
   ],
   "id": "e2eec7935f93c431",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "**************************************************\n",
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "**************************************************\n",
      "1\n",
      "**************************************************\n",
      "2\n",
      "**************************************************\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "**************************************************\n",
      "**************************************************\n",
      "b    1\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "**************************************************\n",
      "a    0\n",
      "d    3\n",
      "dtype: int64\n",
      "**************************************************\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\hx\\AppData\\Local\\Temp\\ipykernel_5616\\1647232830.py:13: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  print(ser_obj[2])\n",
      "C:\\Users\\hx\\AppData\\Local\\Temp\\ipykernel_5616\\1647232830.py:23: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  print(ser_obj[[1,3,4]])\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 布尔索引",
   "id": "7dc1174b9b9686f2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T02:24:04.670126Z",
     "start_time": "2025-03-06T02:24:04.663955Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(ser_obj)\n",
    "print('*' * 50)\n",
    "\n",
    "#布尔索引\n",
    "ser_bool = ser_obj > 2\n",
    "print(ser_bool)\n",
    "print('*' * 50)\n",
    "print(ser_obj[ser_bool])\n",
    "print('*' * 50)\n",
    "print(ser_obj[ser_obj > 2])"
   ],
   "id": "5792b5d328a523a9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "**************************************************\n",
      "a    False\n",
      "b    False\n",
      "c    False\n",
      "d     True\n",
      "e     True\n",
      "dtype: bool\n",
      "**************************************************\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "**************************************************\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# DataFrame索引",
   "id": "4c7fb2d2e2df328f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T02:42:52.409536Z",
     "start_time": "2025-03-06T02:42:52.399124Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# columns指定列索引名\n",
    "import numpy as np\n",
    "\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),columns = ['a','b','c','d'])\n",
    "print(df_obj.head())\n",
    "\n",
    "#列索引\n",
    "print('*' * 50)\n",
    "'''返回Series类型'''\n",
    "print(df_obj['a'])\n",
    "print('*' * 50)\n",
    "'''返回DataFrame类型'''\n",
    "print(df_obj[['a']]) \n",
    "print('*' * 50)\n",
    "print(type(df_obj[['a']]))\n",
    "print('*' * 50)\n",
    "\n",
    "#不连续索引\n",
    "print(df_obj[['a','c']])\n",
    "print('*' * 50)\n",
    "print(type(df_obj[['a','c']]))\n",
    "print('*' * 50)"
   ],
   "id": "1172daf8522e4fbe",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0  0.134788  1.784794  0.165014 -0.002680\n",
      "1 -0.248006  1.257821  0.427873 -1.925470\n",
      "2  0.658943  0.290437  0.543730  0.626465\n",
      "3  0.046976  0.587671  0.242711 -1.350822\n",
      "4  0.399616 -0.138775  0.376906  0.660507\n",
      "**************************************************\n",
      "0    0.134788\n",
      "1   -0.248006\n",
      "2    0.658943\n",
      "3    0.046976\n",
      "4    0.399616\n",
      "Name: a, dtype: float64\n",
      "**************************************************\n",
      "          a\n",
      "0  0.134788\n",
      "1 -0.248006\n",
      "2  0.658943\n",
      "3  0.046976\n",
      "4  0.399616\n",
      "**************************************************\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "**************************************************\n",
      "          a         c\n",
      "0  0.134788  0.165014\n",
      "1 -0.248006  0.427873\n",
      "2  0.658943  0.543730\n",
      "3  0.046976  0.242711\n",
      "4  0.399616  0.376906\n",
      "**************************************************\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 高级索引",
   "id": "570c9fbbe13cb63"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1、loc标签索引（基于标签名的索引",
   "id": "9949563d23fc77ac"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T02:54:03.485241Z",
     "start_time": "2025-03-06T02:54:03.476350Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#Series\n",
    "print(ser_obj)\n",
    "print('*' * 50)\n",
    "print(ser_obj['b':'d'])\n",
    "print('*' * 50)\n",
    "print(ser_obj.loc['b':'d'])\n",
    "print('*' * 50)\n",
    "\n",
    "#DataFrame\n",
    "print(df_obj)\n",
    "print('*' * 50)\n",
    "print(df_obj['a'])\n",
    "print('*' * 50)\n",
    "\n",
    "#第一个参数索引行，第二个参数是列\n",
    "print(df_obj.loc[0:2,'c'])\n",
    "print('*' * 50)"
   ],
   "id": "d2758a3207616941",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "**************************************************\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "**************************************************\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "dtype: int64\n",
      "**************************************************\n",
      "          a         b         c         d\n",
      "0  0.134788  1.784794  0.165014 -0.002680\n",
      "1 -0.248006  1.257821  0.427873 -1.925470\n",
      "2  0.658943  0.290437  0.543730  0.626465\n",
      "3  0.046976  0.587671  0.242711 -1.350822\n",
      "4  0.399616 -0.138775  0.376906  0.660507\n",
      "**************************************************\n",
      "0    0.134788\n",
      "1   -0.248006\n",
      "2    0.658943\n",
      "3    0.046976\n",
      "4    0.399616\n",
      "Name: a, dtype: float64\n",
      "**************************************************\n",
      "0    0.165014\n",
      "1    0.427873\n",
      "2    0.543730\n",
      "Name: c, dtype: float64\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 2、iloc索引（基于索引编号的索引",
   "id": "efd66403260cbcf8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:01:24.676449Z",
     "start_time": "2025-03-06T03:01:24.668082Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#Series\n",
    "print(ser_obj)\n",
    "print('*' * 50)\n",
    "print(ser_obj[1:3])\n",
    "print('*' * 50)\n",
    "print(ser_obj.iloc[1:3])\n",
    "print('*' * 50)\n",
    "\n",
    "#DataFrame\n",
    "print(df_obj)\n",
    "print('*' * 50)\n",
    "print(df_obj.iloc[0:2,0])\n",
    "# #和loc索引对比\n",
    "# print(df_obj.loc[0:2,'c'])"
   ],
   "id": "1fbd5e7def73e97d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n",
      "**************************************************\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "**************************************************\n",
      "b    1\n",
      "c    2\n",
      "dtype: int64\n",
      "**************************************************\n",
      "          a         b         c         d\n",
      "0  0.134788  1.784794  0.165014 -0.002680\n",
      "1 -0.248006  1.257821  0.427873 -1.925470\n",
      "2  0.658943  0.290437  0.543730  0.626465\n",
      "3  0.046976  0.587671  0.242711 -1.350822\n",
      "4  0.399616 -0.138775  0.376906  0.660507\n",
      "**************************************************\n",
      "0    0.134788\n",
      "1   -0.248006\n",
      "Name: a, dtype: float64\n",
      "0    0.165014\n",
      "1    0.427873\n",
      "2    0.543730\n",
      "Name: c, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# pandas的对齐运算",
   "id": "a62bdfdf66d2d488"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1、Series按行、列索引对齐",
   "id": "ed98cfa510bd6b39"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:08:05.344402Z",
     "start_time": "2025-03-06T03:08:05.338890Z"
    }
   },
   "cell_type": "code",
   "source": [
    "s1 = pd.Series(range(10,20),index = range(10))\n",
    "s2 = pd.Series(range(20,25),index = range(5))\n",
    "print('s1:')\n",
    "print(s1)\n",
    "print('')\n",
    "\n",
    "print('s2:')\n",
    "print(s2)\n",
    "print('')"
   ],
   "id": "e37cfe2e02df8c2b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1:\n",
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n",
      "\n",
      "s2:\n",
      "0    20\n",
      "1    21\n",
      "2    22\n",
      "3    23\n",
      "4    24\n",
      "dtype: int64\n",
      "\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 2、Series的对齐运算",
   "id": "f0a082f492b1e0e1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:09:12.821180Z",
     "start_time": "2025-03-06T03:09:12.811647Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('s1 + s2:')\n",
    "print(s1 + s2)"
   ],
   "id": "9af3610bf7018952",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s1 + s2:\n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5     NaN\n",
      "6     NaN\n",
      "7     NaN\n",
      "8     NaN\n",
      "9     NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 3、DataFrame按行列索引对齐",
   "id": "6449e45ae438de7e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:11:30.408458Z",
     "start_time": "2025-03-06T03:11:30.401695Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df1 = pd.DataFrame(np.ones((2,2)),columns = ['a','b'])\n",
    "df2 = pd.DataFrame(np.ones((3,3)),columns = ['a','b','c'])\n",
    "\n",
    "print('df1:')\n",
    "print(df1)\n",
    "print('df2:')\n",
    "print(df2)"
   ],
   "id": "4ab8ae6eec7a46ba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df1:\n",
      "     a    b\n",
      "0  1.0  1.0\n",
      "1  1.0  1.0\n",
      "df2:\n",
      "     a    b    c\n",
      "0  1.0  1.0  1.0\n",
      "1  1.0  1.0  1.0\n",
      "2  1.0  1.0  1.0\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 4、DataFrame的对齐运算",
   "id": "70c4cb459ae8567f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:13:07.132677Z",
     "start_time": "2025-03-06T03:13:07.119762Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#DataFrame的对齐操作\n",
    "print('df1 + df2:')\n",
    "print(df1 + df2)"
   ],
   "id": "5732bd9ea0ce63b3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df1 + df2:\n",
      "     a    b   c\n",
      "0  2.0  2.0 NaN\n",
      "1  2.0  2.0 NaN\n",
      "2  NaN  NaN NaN\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 5、fill_value（使用add,sub,div,mul时，用fill_value指定填充值，未对齐的数据将和填充值做运算",
   "id": "6c7a0d886bdd59c5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:16:39.396497Z",
     "start_time": "2025-03-06T03:16:39.385297Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(s1)\n",
    "print(s2)\n",
    "print(s1.add(s2,fill_value=0))\n",
    "print('*' * 50)\n",
    "\n",
    "print(df1)\n",
    "print(df2)\n",
    "print(df1.sub(df2,fill_value=2))\n",
    "print('*' * 50)"
   ],
   "id": "5a9bc963f44bb007",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n",
      "0    20\n",
      "1    21\n",
      "2    22\n",
      "3    23\n",
      "4    24\n",
      "dtype: int64\n",
      "0    30.0\n",
      "1    32.0\n",
      "2    34.0\n",
      "3    36.0\n",
      "4    38.0\n",
      "5    15.0\n",
      "6    16.0\n",
      "7    17.0\n",
      "8    18.0\n",
      "9    19.0\n",
      "dtype: float64\n",
      "**************************************************\n",
      "     a    b\n",
      "0  1.0  1.0\n",
      "1  1.0  1.0\n",
      "     a    b    c\n",
      "0  1.0  1.0  1.0\n",
      "1  1.0  1.0  1.0\n",
      "2  1.0  1.0  1.0\n",
      "     a    b    c\n",
      "0  0.0  0.0  1.0\n",
      "1  0.0  0.0  1.0\n",
      "2  1.0  1.0  1.0\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Pandas函数应用",
   "id": "da8d476c9827592"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1、可直接使用的NumPy的函数",
   "id": "6f329b7faa9e61e9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:23:48.658741Z",
     "start_time": "2025-03-06T03:23:48.651485Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame(np.random.randn(5,4) - 1)\n",
    "print(df)\n",
    "print('*' * 50)\n",
    "print(np.abs(df)) #取绝对值\n",
    "print('*' * 50)"
   ],
   "id": "be5a0df2c7763a10",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -1.123010 -2.456304 -1.329566 -3.643531\n",
      "1  0.671339 -0.570053 -2.231517 -1.152805\n",
      "2 -0.857705 -0.007749 -0.852853 -1.477923\n",
      "3 -1.895370  0.090331 -0.780955 -0.975421\n",
      "4  0.279893 -1.988402  0.464153  0.598104\n",
      "**************************************************\n",
      "          0         1         2         3\n",
      "0  1.123010  2.456304  1.329566  3.643531\n",
      "1  0.671339  0.570053  2.231517  1.152805\n",
      "2  0.857705  0.007749  0.852853  1.477923\n",
      "3  1.895370  0.090331  0.780955  0.975421\n",
      "4  0.279893  1.988402  0.464153  0.598104\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 2、通过apply将函数应用到列或行上",
   "id": "56f9363be58738af"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:27:17.103239Z",
     "start_time": "2025-03-06T03:27:17.097354Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.apply(lambda x : x.max()))\n",
    "print('*' * 50)\n",
    "\n",
    "#沿指定轴方向\n",
    "print(df.apply(lambda x : x.max(),axis = 1))\n",
    "print('*' * 50)"
   ],
   "id": "1af35c93cfe103cb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.671339\n",
      "1    0.090331\n",
      "2    0.464153\n",
      "3    0.598104\n",
      "dtype: float64\n",
      "**************************************************\n",
      "0   -1.123010\n",
      "1    0.671339\n",
      "2   -0.007749\n",
      "3    0.090331\n",
      "4    0.598104\n",
      "dtype: float64\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 3、通过map将函数应用到每个数据上",
   "id": "e033ed13ffb66ba4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:31:04.616665Z",
     "start_time": "2025-03-06T03:31:04.609563Z"
    }
   },
   "cell_type": "code",
   "source": [
    "f2 = lambda x : '%.2f' % x\n",
    "print(f2)\n",
    "print('*' * 50)\n",
    "print(df.map(f2))\n",
    "print('*' * 50)"
   ],
   "id": "8deec5a1c46b24a8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<function <lambda> at 0x000002AC13512660>\n",
      "**************************************************\n",
      "       0      1      2      3\n",
      "0  -1.12  -2.46  -1.33  -3.64\n",
      "1   0.67  -0.57  -2.23  -1.15\n",
      "2  -0.86  -0.01  -0.85  -1.48\n",
      "3  -1.90   0.09  -0.78  -0.98\n",
      "4   0.28  -1.99   0.46   0.60\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 4、索引排序",
   "id": "254ac7e0f51d8c92"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:37:43.051522Z",
     "start_time": "2025-03-06T03:37:43.039943Z"
    }
   },
   "cell_type": "code",
   "source": [
    "s4 = pd.Series(range(10,15),index = np.random.randint(5,size = 5))\n",
    "print(s4)\n",
    "print('*' * 50)\n",
    "\n",
    "#排序\n",
    "s4.sort_index()\n",
    "# print(s4) #不改变原数据\n",
    "print('*' * 50)\n",
    "\n",
    "#对DataFrame操作时注意轴方向\n",
    "df4 = pd.DataFrame(np.random.randn(3,5),index = np.random.randint(3,size = 3),columns = np.random.randint(5,size = 5))\n",
    "print(df4)\n",
    "print('*' * 50)\n",
    "df4_isort = df4.sort_index(axis = 1,ascending = False)\n",
    "print(df4_isort)\n",
    "print('*' * 50)\n",
    "\n",
    "df4_isort = df4.sort_index(axis = 0,ascending = True)\n",
    "print(df4_isort)\n",
    "print('*' * 50)"
   ],
   "id": "f44847e5de65683e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "4    11\n",
      "4    12\n",
      "1    13\n",
      "2    14\n",
      "dtype: int64\n",
      "**************************************************\n",
      "**************************************************\n",
      "          1         3         1         2         3\n",
      "1 -0.958392 -0.881533 -1.168735 -0.384872  0.626729\n",
      "0 -0.570594  0.873007  1.781962  0.692638  0.316618\n",
      "2  1.588911 -0.221425 -1.302403 -0.612913 -1.492894\n",
      "**************************************************\n",
      "          3         3         2         1         1\n",
      "1 -0.881533  0.626729 -0.384872 -0.958392 -1.168735\n",
      "0  0.873007  0.316618  0.692638 -0.570594  1.781962\n",
      "2 -0.221425 -1.492894 -0.612913  1.588911 -1.302403\n",
      "**************************************************\n",
      "          1         3         1         2         3\n",
      "0 -0.570594  0.873007  1.781962  0.692638  0.316618\n",
      "1 -0.958392 -0.881533 -1.168735 -0.384872  0.626729\n",
      "2  1.588911 -0.221425 -1.302403 -0.612913 -1.492894\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 5、按值排序（by = 'column name'",
   "id": "9ae24d2696fb6b65"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T03:43:41.133477Z",
     "start_time": "2025-03-06T03:43:41.123574Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df4)\n",
    "print('*' * 50)\n",
    "\n",
    "df4_vsort = df4.sort_values(by = 2, ascending = False)\n",
    "print(df4_vsort)\n",
    "print('*' * 50)"
   ],
   "id": "328905b97553fb59",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1         3         1         2         3\n",
      "1 -0.958392 -0.881533 -1.168735 -0.384872  0.626729\n",
      "0 -0.570594  0.873007  1.781962  0.692638  0.316618\n",
      "2  1.588911 -0.221425 -1.302403 -0.612913 -1.492894\n",
      "**************************************************\n",
      "          1         3         1         2         3\n",
      "0 -0.570594  0.873007  1.781962  0.692638  0.316618\n",
      "1 -0.958392 -0.881533 -1.168735 -0.384872  0.626729\n",
      "2  1.588911 -0.221425 -1.302403 -0.612913 -1.492894\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 处理缺失数据",
   "id": "f77118108fa8ae7d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T04:05:12.305717Z",
     "start_time": "2025-03-06T04:05:12.298717Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_data = pd.DataFrame([np.random.randn(3),[1.,2.,np.nan],[np.nan,4.,np.nan],[1.,2.,3.]])\n",
    "print(df_data.head())"
   ],
   "id": "2b12e830ac90bd0e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0 -1.920303  1.383019 -0.370482\n",
      "1  1.000000  2.000000       NaN\n",
      "2       NaN  4.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1、判断有无缺失值 null()",
   "id": "52bf8c8747a6f636"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T06:26:52.215439Z",
     "start_time": "2025-03-06T06:26:52.209848Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_data.head())\n",
    "print('*' * 50)\n",
    "print(df_data.isnull())\n",
    "print('*' * 50)"
   ],
   "id": "9425eea0d43465a9",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0 -1.920303  1.383019 -0.370482\n",
      "1  1.000000  2.000000       NaN\n",
      "2       NaN  4.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n",
      "**************************************************\n",
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n"
     ]
    }
   ],
   "execution_count": 59
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 2、丢失缺失的数据 dropna()",
   "id": "c77b61e344e75b8f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T06:27:29.032332Z",
     "start_time": "2025-03-06T06:27:29.024581Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_data.head())\n",
    "print('*' * 50)\n",
    "print(df_data.dropna())\n",
    "print('*' * 50)\n",
    "print(df_data.dropna(axis = 1))\n",
    "print('*' * 50)"
   ],
   "id": "25ae877d0369adef",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0 -1.920303  1.383019 -0.370482\n",
      "1  1.000000  2.000000       NaN\n",
      "2       NaN  4.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n",
      "**************************************************\n",
      "          0         1         2\n",
      "0 -1.920303  1.383019 -0.370482\n",
      "3  1.000000  2.000000  3.000000\n",
      "**************************************************\n",
      "          1\n",
      "0  1.383019\n",
      "1  2.000000\n",
      "2  4.000000\n",
      "3  2.000000\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 3、填充缺失数据 fillna()",
   "id": "60dcf337e7b758d1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T06:28:36.939052Z",
     "start_time": "2025-03-06T06:28:36.933706Z"
    }
   },
   "cell_type": "code",
   "source": "print(df_data.fillna(-100.))",
   "id": "8e72bad99d586c5c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            0         1           2\n",
      "0   -1.920303  1.383019   -0.370482\n",
      "1    1.000000  2.000000 -100.000000\n",
      "2 -100.000000  4.000000 -100.000000\n",
      "3    1.000000  2.000000    3.000000\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Pandas统计计算和描述",
   "id": "a0ab06d58eef04cb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T06:45:05.053939Z",
     "start_time": "2025-03-06T06:45:05.047335Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#示例代码\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "df_obj = pd.DataFrame(np.random.randn(5,4),columns = ['a','b','c','d'])\n",
    "print(df_obj.head())"
   ],
   "id": "47bb184aaa89a0db",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0  0.413401 -0.837780 -1.017035 -0.048282\n",
      "1 -1.494739  0.080881  0.001010  0.848765\n",
      "2  0.651316 -1.381123  0.869409  4.149576\n",
      "3 -0.505626 -0.171755 -0.497964 -0.333026\n",
      "4 -2.170070  0.273711  0.869022 -0.656206\n"
     ]
    }
   ],
   "execution_count": 64
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1、sun,mean,max,min,etc",
   "id": "ea3c889bd3da300b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T06:49:24.768292Z",
     "start_time": "2025-03-06T06:49:24.760677Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_obj.head())\n",
    "print('*' * 50)\n",
    "print(df_obj.sum())\n",
    "print('*' * 50)\n",
    "print(df_obj.max(axis = 1))\n",
    "print('*' * 50)\n",
    "print(df_obj.min(axis = 1,skipna = False)) #skipna:排除缺失值，默认为True\n",
    "print('*' * 50)"
   ],
   "id": "1d0b6c21de42bd5c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0  0.413401 -0.837780 -1.017035 -0.048282\n",
      "1 -1.494739  0.080881  0.001010  0.848765\n",
      "2  0.651316 -1.381123  0.869409  4.149576\n",
      "3 -0.505626 -0.171755 -0.497964 -0.333026\n",
      "4 -2.170070  0.273711  0.869022 -0.656206\n",
      "**************************************************\n",
      "a   -3.105719\n",
      "b   -2.036065\n",
      "c    0.224441\n",
      "d    3.960826\n",
      "dtype: float64\n",
      "**************************************************\n",
      "0    0.413401\n",
      "1    0.848765\n",
      "2    4.149576\n",
      "3   -0.171755\n",
      "4    0.869022\n",
      "dtype: float64\n",
      "**************************************************\n",
      "a   -2.170070\n",
      "b   -1.381123\n",
      "c   -1.017035\n",
      "d   -0.656206\n",
      "dtype: float64\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 68
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 2、describe产生多个数据",
   "id": "79731618112bc3c0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T06:51:23.889558Z",
     "start_time": "2025-03-06T06:51:23.876691Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_obj.head())\n",
    "print('*' * 50)\n",
    "print(df_obj.describe())\n",
    "print('*' * 50)"
   ],
   "id": "220b304347fe701a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0  0.413401 -0.837780 -1.017035 -0.048282\n",
      "1 -1.494739  0.080881  0.001010  0.848765\n",
      "2  0.651316 -1.381123  0.869409  4.149576\n",
      "3 -0.505626 -0.171755 -0.497964 -0.333026\n",
      "4 -2.170070  0.273711  0.869022 -0.656206\n",
      "**************************************************\n",
      "              a         b         c         d\n",
      "count  5.000000  5.000000  5.000000  5.000000\n",
      "mean  -0.621144 -0.407213  0.044888  0.792165\n",
      "std    1.210901  0.687609  0.834165  1.958673\n",
      "min   -2.170070 -1.381123 -1.017035 -0.656206\n",
      "25%   -1.494739 -0.837780 -0.497964 -0.333026\n",
      "50%   -0.505626 -0.171755  0.001010 -0.048282\n",
      "75%    0.413401  0.080881  0.869022  0.848765\n",
      "max    0.651316  0.273711  0.869409  4.149576\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 72
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 索引",
   "id": "59cd7f0865a82c31"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1、不同情况紧急情况的次数",
   "id": "a487af376c380a65"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T08:46:45.018201Z",
     "start_time": "2025-03-06T08:46:43.309821Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "file_path = \"./911.csv\"\n",
    "df = pd.read_csv(file_path)\n",
    "\n",
    "'''获取分类'''\n",
    "# print(df[\"title\"].str.split(\":\"))\n",
    "temp_list = df[\"title\"].str.split(\":\").tolist()\n",
    "cate_list = list(set([i[0] for i in temp_list])) #分类\n",
    "print(cate_list)\n",
    "print('*' * 100)\n",
    "\n",
    "'''构造全为0的数组'''\n",
    "zeros_df = pd.DataFrame(np.zeros((df.shape[0],len(cate_list))),columns = cate_list)\n",
    "print(zeros_df)\n",
    "print('*' * 100)\n",
    "\n",
    "# 赋值，对未赋值True的地方赋值为1\n",
    "for cate in cate_list:\n",
    "    zeros_df[cate][df[\"title\"].str.contains(cate)] = 1\n",
    "# print(zeros_df)\n",
    "# print('*' * 100)\n",
    "\n",
    "#求和即可得到\n",
    "sum_ret = zeros_df.sum(axis = 0)\n",
    "print(sum_ret)"
   ],
   "id": "de2f5c94bbb0d208",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['EMS', 'Traffic', 'Fire']\n",
      "****************************************************************************************************\n",
      "        EMS  Traffic  Fire\n",
      "0       0.0      0.0   0.0\n",
      "1       0.0      0.0   0.0\n",
      "2       0.0      0.0   0.0\n",
      "3       0.0      0.0   0.0\n",
      "4       0.0      0.0   0.0\n",
      "...     ...      ...   ...\n",
      "249732  0.0      0.0   0.0\n",
      "249733  0.0      0.0   0.0\n",
      "249734  0.0      0.0   0.0\n",
      "249735  0.0      0.0   0.0\n",
      "249736  0.0      0.0   0.0\n",
      "\n",
      "[249737 rows x 3 columns]\n",
      "****************************************************************************************************\n",
      "EMS        124844.0\n",
      "Traffic     87465.0\n",
      "Fire        37432.0\n",
      "dtype: float64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\hx\\AppData\\Local\\Temp\\ipykernel_5616\\1534366286.py:22: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "  zeros_df[cate][df[\"title\"].str.contains(cate)] = 1\n",
      "C:\\Users\\hx\\AppData\\Local\\Temp\\ipykernel_5616\\1534366286.py:22: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "  zeros_df[cate][df[\"title\"].str.contains(cate)] = 1\n",
      "C:\\Users\\hx\\AppData\\Local\\Temp\\ipykernel_5616\\1534366286.py:22: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "  zeros_df[cate][df[\"title\"].str.contains(cate)] = 1\n"
     ]
    }
   ],
   "execution_count": 87
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 2、另法",
   "id": "ceafdb234bc715c6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T08:54:46.918268Z",
     "start_time": "2025-03-06T08:54:45.798762Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "df = pd.read_csv(\"./911.csv\")\n",
    "print(df.head(5))\n",
    "print('*' * 100)\n",
    "\n",
    "#获取分类\n",
    "temp_list = df[\"title\"].str.split(\":\").tolist()\n",
    "print(temp_list[1:5])\n",
    "print('*' * 100)\n",
    "cate_list = [i[0] for i in temp_list]\n",
    "#多加一列cate元素\n",
    "df['cate'] = pd.DataFrame(np.array(cate_list).reshape((df.shape[0],1)))\n",
    "# print(df.head(5))\n",
    "print(df.groupby(by = \"cate\").count()[\"title\"])"
   ],
   "id": "588beed61a7f482e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         lat        lng                                               desc  \\\n",
      "0  40.297876 -75.581294  REINDEER CT & DEAD END;  NEW HANOVER; Station ...   \n",
      "1  40.258061 -75.264680  BRIAR PATH & WHITEMARSH LN;  HATFIELD TOWNSHIP...   \n",
      "2  40.121182 -75.351975  HAWS AVE; NORRISTOWN; 2015-12-10 @ 14:39:21-St...   \n",
      "3  40.116153 -75.343513  AIRY ST & SWEDE ST;  NORRISTOWN; Station 308A;...   \n",
      "4  40.251492 -75.603350  CHERRYWOOD CT & DEAD END;  LOWER POTTSGROVE; S...   \n",
      "\n",
      "       zip                    title            timeStamp                twp  \\\n",
      "0  19525.0   EMS: BACK PAINS/INJURY  2015-12-10 17:10:52        NEW HANOVER   \n",
      "1  19446.0  EMS: DIABETIC EMERGENCY  2015-12-10 17:29:21  HATFIELD TOWNSHIP   \n",
      "2  19401.0      Fire: GAS-ODOR/LEAK  2015-12-10 14:39:21         NORRISTOWN   \n",
      "3  19401.0   EMS: CARDIAC EMERGENCY  2015-12-10 16:47:36         NORRISTOWN   \n",
      "4      NaN           EMS: DIZZINESS  2015-12-10 16:56:52   LOWER POTTSGROVE   \n",
      "\n",
      "                         addr  e  \n",
      "0      REINDEER CT & DEAD END  1  \n",
      "1  BRIAR PATH & WHITEMARSH LN  1  \n",
      "2                    HAWS AVE  1  \n",
      "3          AIRY ST & SWEDE ST  1  \n",
      "4    CHERRYWOOD CT & DEAD END  1  \n",
      "****************************************************************************************************\n",
      "[['EMS', ' DIABETIC EMERGENCY'], ['Fire', ' GAS-ODOR/LEAK'], ['EMS', ' CARDIAC EMERGENCY'], ['EMS', ' DIZZINESS']]\n",
      "****************************************************************************************************\n",
      "cate\n",
      "EMS        124840\n",
      "Fire        37432\n",
      "Traffic     87465\n",
      "Name: title, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 93
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 时间序列",
   "id": "e8df75301d1a4137"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T13:46:41.468596Z",
     "start_time": "2025-03-06T13:46:41.439879Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "\n",
    "pd.date_range(start = '20250306',end = '20250831')"
   ],
   "id": "7c5aadf0ec7931c6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2025-03-06', '2025-03-07', '2025-03-08', '2025-03-09',\n",
       "               '2025-03-10', '2025-03-11', '2025-03-12', '2025-03-13',\n",
       "               '2025-03-14', '2025-03-15',\n",
       "               ...\n",
       "               '2025-08-22', '2025-08-23', '2025-08-24', '2025-08-25',\n",
       "               '2025-08-26', '2025-08-27', '2025-08-28', '2025-08-29',\n",
       "               '2025-08-30', '2025-08-31'],\n",
       "              dtype='datetime64[ns]', length=179, freq='D')"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 95
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# pandas重采样（降采样，升采样",
   "id": "343c22aaa374b516"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T13:50:57.992701Z",
     "start_time": "2025-03-06T13:50:56.428659Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#911数据中不同月份不同类型的电话的次数的变化情况\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "#把时间字符串转为时间类型设置为索引\n",
    "df = pd.read_csv(\"./911.csv\")\n",
    "print(df.head())\n",
    "print('*' * 100)\n",
    "df[\"timeStamp\"] = pd.to_datetime(df[\"timeStamp\"])\n",
    "print(df.head())\n",
    "print('*' * 100)"
   ],
   "id": "4acd7485bb52976f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         lat        lng                                               desc  \\\n",
      "0  40.297876 -75.581294  REINDEER CT & DEAD END;  NEW HANOVER; Station ...   \n",
      "1  40.258061 -75.264680  BRIAR PATH & WHITEMARSH LN;  HATFIELD TOWNSHIP...   \n",
      "2  40.121182 -75.351975  HAWS AVE; NORRISTOWN; 2015-12-10 @ 14:39:21-St...   \n",
      "3  40.116153 -75.343513  AIRY ST & SWEDE ST;  NORRISTOWN; Station 308A;...   \n",
      "4  40.251492 -75.603350  CHERRYWOOD CT & DEAD END;  LOWER POTTSGROVE; S...   \n",
      "\n",
      "       zip                    title            timeStamp                twp  \\\n",
      "0  19525.0   EMS: BACK PAINS/INJURY  2015-12-10 17:10:52        NEW HANOVER   \n",
      "1  19446.0  EMS: DIABETIC EMERGENCY  2015-12-10 17:29:21  HATFIELD TOWNSHIP   \n",
      "2  19401.0      Fire: GAS-ODOR/LEAK  2015-12-10 14:39:21         NORRISTOWN   \n",
      "3  19401.0   EMS: CARDIAC EMERGENCY  2015-12-10 16:47:36         NORRISTOWN   \n",
      "4      NaN           EMS: DIZZINESS  2015-12-10 16:56:52   LOWER POTTSGROVE   \n",
      "\n",
      "                         addr  e  \n",
      "0      REINDEER CT & DEAD END  1  \n",
      "1  BRIAR PATH & WHITEMARSH LN  1  \n",
      "2                    HAWS AVE  1  \n",
      "3          AIRY ST & SWEDE ST  1  \n",
      "4    CHERRYWOOD CT & DEAD END  1  \n",
      "****************************************************************************************************\n",
      "         lat        lng                                               desc  \\\n",
      "0  40.297876 -75.581294  REINDEER CT & DEAD END;  NEW HANOVER; Station ...   \n",
      "1  40.258061 -75.264680  BRIAR PATH & WHITEMARSH LN;  HATFIELD TOWNSHIP...   \n",
      "2  40.121182 -75.351975  HAWS AVE; NORRISTOWN; 2015-12-10 @ 14:39:21-St...   \n",
      "3  40.116153 -75.343513  AIRY ST & SWEDE ST;  NORRISTOWN; Station 308A;...   \n",
      "4  40.251492 -75.603350  CHERRYWOOD CT & DEAD END;  LOWER POTTSGROVE; S...   \n",
      "\n",
      "       zip                    title           timeStamp                twp  \\\n",
      "0  19525.0   EMS: BACK PAINS/INJURY 2015-12-10 17:10:52        NEW HANOVER   \n",
      "1  19446.0  EMS: DIABETIC EMERGENCY 2015-12-10 17:29:21  HATFIELD TOWNSHIP   \n",
      "2  19401.0      Fire: GAS-ODOR/LEAK 2015-12-10 14:39:21         NORRISTOWN   \n",
      "3  19401.0   EMS: CARDIAC EMERGENCY 2015-12-10 16:47:36         NORRISTOWN   \n",
      "4      NaN           EMS: DIZZINESS 2015-12-10 16:56:52   LOWER POTTSGROVE   \n",
      "\n",
      "                         addr  e  \n",
      "0      REINDEER CT & DEAD END  1  \n",
      "1  BRIAR PATH & WHITEMARSH LN  1  \n",
      "2                    HAWS AVE  1  \n",
      "3          AIRY ST & SWEDE ST  1  \n",
      "4    CHERRYWOOD CT & DEAD END  1  \n",
      "****************************************************************************************************\n"
     ]
    }
   ],
   "execution_count": 96
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数据连接（merge",
   "id": "cc5d26c6ba566e29"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T14:08:13.364823Z",
     "start_time": "2025-03-06T14:08:13.334943Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df_obj1 = pd.DataFrame({'key' : ['b','b','a','c','a','a','b'],\n",
    "                        'data1' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'key' : ['a','b','d'],\n",
    "                        'data2' : np.random.randint(0,10,3)})\n",
    "print(df_obj1.head())\n",
    "print('*' * 50)\n",
    "print(df_obj2.head())\n",
    "print('*' * 50)\n",
    "\n",
    "#默认将重叠列的列名作为“外键”进行连接\n",
    "print(pd.merge(df_obj1, df_obj2))\n",
    "print('*' * 50)\n",
    "\n",
    "#on显示指定“外键”\n",
    "print(pd.merge(df_obj1, df_obj2,on = 'key'))\n",
    "print('*' * 50)\n",
    "\n",
    "#left_on左侧的外键，right_on右侧的外键\n",
    "df_obj1 = df_obj1.rename(columns={'key':'key1'})\n",
    "df_obj2 = df_obj2.rename(columns={'key':'key2'})\n",
    "\n",
    "print(pd.merge(df_obj1, df_obj2, left_on = 'key1', right_on= 'key2'))\n",
    "print('*' * 50)\n",
    "\n",
    "''' 外连接（outer）结果中的键是并集 '''\n",
    "print(pd.merge(df_obj1, df_obj2, left_on = 'key1', right_on= 'key2',how = 'outer'))\n",
    "print('*' * 50)\n",
    "'''左连接'''\n",
    "print(pd.merge(df_obj1,df_obj2,left_on = 'key1', right_on= 'key2',how = 'left'))\n",
    "print('*' * 50)\n",
    "'''右连接'''\n",
    "print(pd.merge(df_obj1,df_obj2,left_on = 'key1', right_on= 'key2',how = 'right'))\n",
    "print('*' * 50)"
   ],
   "id": "69935b80bf0d9f2e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1\n",
      "0   b      9\n",
      "1   b      5\n",
      "2   a      1\n",
      "3   c      4\n",
      "4   a      7\n",
      "**************************************************\n",
      "  key  data2\n",
      "0   a      6\n",
      "1   b      0\n",
      "2   d      8\n",
      "**************************************************\n",
      "  key  data1  data2\n",
      "0   b      9      0\n",
      "1   b      5      0\n",
      "2   a      1      6\n",
      "3   a      7      6\n",
      "4   a      3      6\n",
      "5   b      6      0\n",
      "**************************************************\n",
      "  key  data1  data2\n",
      "0   b      9      0\n",
      "1   b      5      0\n",
      "2   a      1      6\n",
      "3   a      7      6\n",
      "4   a      3      6\n",
      "5   b      6      0\n",
      "**************************************************\n",
      "  key1  data1 key2  data2\n",
      "0    b      9    b      0\n",
      "1    b      5    b      0\n",
      "2    a      1    a      6\n",
      "3    a      7    a      6\n",
      "4    a      3    a      6\n",
      "5    b      6    b      0\n",
      "**************************************************\n",
      "  key1  data1 key2  data2\n",
      "0    a    1.0    a    6.0\n",
      "1    a    7.0    a    6.0\n",
      "2    a    3.0    a    6.0\n",
      "3    b    9.0    b    0.0\n",
      "4    b    5.0    b    0.0\n",
      "5    b    6.0    b    0.0\n",
      "6    c    4.0  NaN    NaN\n",
      "7  NaN    NaN    d    8.0\n",
      "**************************************************\n",
      "  key1  data1 key2  data2\n",
      "0    b      9    b    0.0\n",
      "1    b      5    b    0.0\n",
      "2    a      1    a    6.0\n",
      "3    c      4  NaN    NaN\n",
      "4    a      7    a    6.0\n",
      "5    a      3    a    6.0\n",
      "6    b      6    b    0.0\n",
      "**************************************************\n",
      "  key1  data1 key2  data2\n",
      "0    a    1.0    a      6\n",
      "1    a    7.0    a      6\n",
      "2    a    3.0    a      6\n",
      "3    b    9.0    b      0\n",
      "4    b    5.0    b      0\n",
      "5    b    6.0    b      0\n",
      "6  NaN    NaN    d      8\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 103
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 处理重复列名",
   "id": "73fd6e591b168539"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T14:15:01.276731Z",
     "start_time": "2025-03-06T14:15:01.263319Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#suffixes,默认为_x,_y给数据添加后缀实现别名\n",
    "df_obj1 = pd.DataFrame({'key' : ['b','b','a','c','a','a','b'],'data' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'key' : ['a','b','d'],'data' : np.random.randint(0,10,3)})\n",
    "print(df_obj1.head())\n",
    "print('*' * 50)\n",
    "print(df_obj2.head())\n",
    "print('*' * 50)\n",
    "print(pd.merge(df_obj1,df_obj2,on = 'key',suffixes = ('_left','_right')))"
   ],
   "id": "976098f2032eac5d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data\n",
      "0   b     9\n",
      "1   b     3\n",
      "2   a     3\n",
      "3   c     7\n",
      "4   a     0\n",
      "**************************************************\n",
      "  key  data\n",
      "0   a     0\n",
      "1   b     5\n",
      "2   d     9\n",
      "**************************************************\n",
      "  key  data_left  data_right\n",
      "0   b          9           5\n",
      "1   b          3           5\n",
      "2   a          3           0\n",
      "3   a          0           0\n",
      "4   a          8           0\n",
      "5   b          1           5\n"
     ]
    }
   ],
   "execution_count": 105
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 按索引链接",
   "id": "fc05521878a7b704"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T14:20:18.010342Z",
     "start_time": "2025-03-06T14:20:17.999533Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#left_index = True 或 right_index = True\n",
    "df_obj1 = pd.DataFrame({'key' : ['b','b','a','c','a','a','b'],'data1' : np.random.randint(0,10,7)})\n",
    "df_obj2 = pd.DataFrame({'data2' : np.random.randint(0,10,3)},index = ['a','b','d'])\n",
    "print(df_obj1.head())\n",
    "print('*' * 50)\n",
    "print(df_obj2.head())\n",
    "print('*' * 50)\n",
    "print(pd.merge(df_obj1,df_obj2,left_on = 'key',right_index = True))"
   ],
   "id": "a4b78aa302f9e70b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  key  data1\n",
      "0   b      7\n",
      "1   b      9\n",
      "2   a      5\n",
      "3   c      6\n",
      "4   a      5\n",
      "**************************************************\n",
      "   data2\n",
      "a      6\n",
      "b      3\n",
      "d      0\n",
      "**************************************************\n",
      "  key  data1  data2\n",
      "0   b      7      3\n",
      "1   b      9      3\n",
      "2   a      5      6\n",
      "4   a      5      6\n",
      "5   a      2      6\n",
      "6   b      2      3\n"
     ]
    }
   ],
   "execution_count": 108
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 重复数据处理",
   "id": "b89a5df898e69af0"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1、处理重复数据",
   "id": "80caa1ebae6587cd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T14:43:29.311320Z",
     "start_time": "2025-03-06T14:43:29.299965Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#1. duplicated()返回布尔型Series每行是否为重复行\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "df_obj = pd.DataFrame({'data1' : ['a'] * 4 + ['b'] * 4,\n",
    "                       'data2' : np.random.randint(0,4,8)})\n",
    "print(df_obj)\n",
    "print('*' * 50)\n",
    "print(df_obj.duplicated())\n",
    "print('*' * 50)\n",
    "\n",
    "#drop_duplicates()过滤重复行\n",
    "print(df_obj.drop_duplicates())\n",
    "print('*' * 50)\n",
    "print(df_obj.drop_duplicates('data2'))\n",
    "print('*' * 50)\n",
    "\n",
    "#根据map传入的函数对每行或每列进行转换\n",
    "ser_obj = pd.Series(np.random.randint(0,10,10))\n",
    "print(ser_obj)\n",
    "print('*' * 50)\n",
    "print(ser_obj.map(lambda x : x * 2))"
   ],
   "id": "50abd24c3ec3b2f6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  data1  data2\n",
      "0     a      0\n",
      "1     a      1\n",
      "2     a      1\n",
      "3     a      2\n",
      "4     b      3\n",
      "5     b      1\n",
      "6     b      2\n",
      "7     b      1\n",
      "**************************************************\n",
      "0    False\n",
      "1    False\n",
      "2     True\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "6    False\n",
      "7     True\n",
      "dtype: bool\n",
      "**************************************************\n",
      "  data1  data2\n",
      "0     a      0\n",
      "1     a      1\n",
      "3     a      2\n",
      "4     b      3\n",
      "5     b      1\n",
      "6     b      2\n",
      "**************************************************\n",
      "  data1  data2\n",
      "0     a      0\n",
      "1     a      1\n",
      "3     a      2\n",
      "4     b      3\n",
      "**************************************************\n",
      "0    8\n",
      "1    0\n",
      "2    4\n",
      "3    2\n",
      "4    7\n",
      "5    7\n",
      "6    1\n",
      "7    1\n",
      "8    8\n",
      "9    4\n",
      "dtype: int32\n",
      "**************************************************\n",
      "0    16\n",
      "1     0\n",
      "2     8\n",
      "3     4\n",
      "4    14\n",
      "5    14\n",
      "6     2\n",
      "7     2\n",
      "8    16\n",
      "9     8\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 113
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 2、数据替换",
   "id": "4fd2dd28364dc87e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-06T14:50:29.480717Z",
     "start_time": "2025-03-06T14:50:29.474603Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''replace根据值的内容进行替换'''\n",
    "print(ser_obj)\n",
    "print('*' * 50)\n",
    "\n",
    "#单个值替换成单个值\n",
    "print(ser_obj.replace(1,-100))\n",
    "print('*' * 50)\n",
    "\n",
    "#多个值替换成一个值\n",
    "print(ser_obj.replace([6,8],-100))\n",
    "print('*' * 50)\n",
    "\n",
    "#多个值替换为多个值\n",
    "print(ser_obj.replace([1,8],[666,888]))\n",
    "print('*' * 50)"
   ],
   "id": "39197fe82c3c00cc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    8\n",
      "1    0\n",
      "2    4\n",
      "3    2\n",
      "4    7\n",
      "5    7\n",
      "6    1\n",
      "7    1\n",
      "8    8\n",
      "9    4\n",
      "dtype: int32\n",
      "**************************************************\n",
      "0      8\n",
      "1      0\n",
      "2      4\n",
      "3      2\n",
      "4      7\n",
      "5      7\n",
      "6   -100\n",
      "7   -100\n",
      "8      8\n",
      "9      4\n",
      "dtype: int32\n",
      "**************************************************\n",
      "0   -100\n",
      "1      0\n",
      "2      4\n",
      "3      2\n",
      "4      7\n",
      "5      7\n",
      "6      1\n",
      "7      1\n",
      "8   -100\n",
      "9      4\n",
      "dtype: int32\n",
      "**************************************************\n",
      "0      8\n",
      "1      0\n",
      "2      4\n",
      "3      2\n",
      "4    888\n",
      "5    888\n",
      "6    666\n",
      "7    666\n",
      "8      8\n",
      "9      4\n",
      "dtype: int32\n",
      "**************************************************\n"
     ]
    }
   ],
   "execution_count": 116
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "cdcdd442d0507588"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
